<template>
	<view class="container-conversation">
		<u-navbar :is-back="false" title="社群">
			<view class="slot-wrap" @click="fanh">
				<!-- <image src="@/static/IM/left-icon.png" style="width:20rpx;height:40rpx;margin-left:20rpx;"></image> -->
			</view>
			<view slot="right" class="right-icon" @click="goHaoy">
				<image src="@/static/IM/haoy.png" style="width:40rpx;height:40rpx;margin-right:20rpx;"></image>
			</view>
		</u-navbar>
		<view class="scroll-box">
			<view class="uni-list margintop-bar">
				<!-- <view v-for="item in conversationList" :key="item.conversationID"
					@tap="handleRoute(item.conversationID)">
					<TUI-conversation-item :data-type="item.type" :conversation="item"></TUI-conversation-item>
				</view> -->
				<u-swipe-action :show="item.show" :index="index" v-for="(item, index) in list" :key="item.id"
					@click="click" @open="open" :options="options">
					<view class="item u-border-bottom">
						<image mode="aspectFill" :src="item.images" />
						<!-- 此层wrap在此为必写的，否则可能会出现标题定位错误 -->
						<view class="title-wrap">
							<text class="title u-line-2">{{ item.title }}</text>
						</view>
					</view>
				</u-swipe-action>
			</view>
		</view>
		<view class="bottom-back">
			<view class="bottom-area">
				<view v-if="showSelectTag" class="conversation-bubble" @tap.stop="handleEditToggle">
					<view v-for="(item, index) in array" :key="index" class="picker" :data-name="item.name"
						@tap="handleOnTap">{{ item.name }}</view>
				</view>
				<image @tap="showMore" class="btn-show-more" src="/static/static/assets/add.svg"></image>
				<view @tap="learnMore" class="im-link">了解更多IM功能</view>
			</view>
		</view>
	</view>
</template>
<!-- 这里有个加载的补丁逻辑待优化，页面向SDK请求conversationList的时候会有时延，
造成页面的一个抖动，这里加一个if逻辑打一个补丁，后续继续优化 -->
<script>
	import logger from '@/utils/logger';
	import TUIConversationItem from '@/components/tui-conversation/conversation-item/index';
	import TUIMessageList from '@/components/tui-chat/message-list/index';

	export default {
		data() {
			return {
				conversationList: [],
				showSelectTag: false,
				array: [{
						name: '发起群聊'
					},
					{
						name: '加入群聊'
					}
				],
				list: [{
						id: 1,
						title: '长安回望绣成堆，山顶千门次第开，一骑红尘妃子笑，无人知是荔枝来',
						images: 'https://cdn.uviewui.com/uview/common/logo.png',
						show: false
					},
					{
						id: 2,
						title: '新丰绿树起黄埃，数骑渔阳探使回，霓裳一曲千峰上，舞破中原始下来',
						images: 'https://cdn.uviewui.com/uview/common/logo.png',
						show: false
					},
					{
						id: 3,
						title: '登临送目，正故国晚秋，天气初肃。千里澄江似练，翠峰如簇',
						images: 'https://cdn.uviewui.com/uview/common/logo.png',
						show: false,
					}
				],
				disabled: false,
				btnWidth: 180,
				show: false,
				options: [{
						text: '收藏',
						style: {
							backgroundColor: '#007aff'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				]
			};
		},

		components: {
			TUIConversationItem,
			TUIMessageList
		},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad() {
			// 登入后拉去会话列表
			this.getConversationList();
			uni.$TUIKit.on(uni.$TUIKitEvent.CONVERSATION_LIST_UPDATED, this.onConversationListUpdated);
		},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload() {
			uni.$TUIKit.off(uni.$TUIKitEvent.SDK_READY, this.onConversationListUpdated);
		},

		methods: {
			click(index, index1) {
				if (index1 == 1) {
					this.list.splice(index, 1);
					this.$u.toast(`删除了第${index}个cell`);
				} else {
					this.list[index].show = false;
					this.$u.toast(`收藏成功`);
				}
			},
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			open(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				this.list[index].show = true;
				this.list.map((val, idx) => {
					if (index != idx) this.list[idx].show = false;
				})
			},
			handleRoute(id) {
				const url = `/pages/subpackage/IM/TUI-Chat/chat?conversationID=${id}`;
				uni.navigateTo({
					url
				});
			},
			onConversationListUpdated(event) {
				logger.log('TUI-conversation | onConversationListUpdated  |ok');
				this.setData({
					conversationList: event.data
				});
			},

			getConversationList() {
				uni.$TUIKit.getConversationList().then(imResponse => {
					logger.log(
						`TUI-conversation | getConversationList | getConversationList-length: ${imResponse.data.conversationList.length}`
					);
					this.setData({
						conversationList: imResponse.data.conversationList
					});
				});
			},

			showMore() {
				this.setData({
					showSelectTag: !this.showSelectTag
				});
			},

			learnMore() {
				uni.navigateTo({
					url: '../../TUI-User-Center/webview/webview?url=https://cloud.tencent.com/product/im'
				});
			},

			handleOnTap(event) {
				this.setData({
						showSelectTag: false
					},
					() => {
						switch (event.currentTarget.dataset.name) {
							case '发起群聊':
								this.$createGroup();
								break;

							case '加入群聊':
								this.$joinGroup();
							default:
								break;
						}
					}
				);
			},

			goHomePage() {
				// uni.navigateTo 不能跳转到 tabbar 页面，使用 uni.switchTab 代替
				uni.switchTab({
					url: '../../TUI-Index/index'
				});
			},

			handleEditToggle() {
				this.setData({
					showSelectTag: false
				});
			},

			$createConversation() {
				uni.navigateTo({
					url: '../create-conversation/create'
				});
			},

			$createGroup() {
				uni.navigateTo({
					url: '/pages/subpackage/IM/TUI-Group/create-group/create'
				});
			},

			$joinGroup() {
				uni.navigateTo({
					url: '/pages/subpackage/IM/TUI-Group/join-group/join'
				});
			},
			goHaoy() {
				uni.navigateTo({
					url: '/pages/subpackage/IM/haoy/haoy'
				})
			},
			fanh() {
				console.log('触发')
				uni.$TUIKit.logout().then(imResponse => {
					uni.showToast({
						title: "退出成功",
						icon: "none",
						duration: 2000,
					});
				});
				uni.navigateBack({
					delta: 1
				})
			}
		}
	};
</script>
<style scoped lang="scss">
	@import './conversation.css';

	.container-conversation {
		.item {
			display: flex;
			padding: 20rpx;
		}

		image {
			width: 120rpx;
			flex: 0 0 120rpx;
			height: 120rpx;
			margin-right: 20rpx;
			border-radius: 12rpx;
		}

		.title {
			text-align: left;
			font-size: 28rpx;
			color: $u-content-color;
			margin-top: 20rpx;
		}
	}
</style>